/**
 * 常用方法封装【基础类】
 * @author 
 * @date 2018-07-13
 */
layui.define(['form','layer','laydate'],function(exports){
	"use strict";
	var form = layui.form,
		layer = layer = parent.layer === undefined ? layui.layer : top.layer,
		laydate = layui.laydate,
		$ = layui.$;
	
	/**
	 * 自定义模块接口对象
	 */
	var active = {
		/**
		 * 添加、编辑公共函数
		 */
		edit: function(title,id,tW=0,tH=0,param){
			var titleStr = '';
	        if(id>0){
	        	titleStr = '修改';
	        }else{
	        	titleStr = '新增';
	        }
	        if(title=='undefined' || title=='') {
	        	titleStr += "信息";
	        }else{
	        	titleStr += title;
	        }
	        
	        //URL逻辑处理
	        var url = cUrl+"/edit?id="+id;
	        if (Array.isArray(param)) {
			    for (var i in param) {
			    	console.log("索引："+i+",数据源："+param[i]);
			    	url += "&"+param[i];
	            }
			}
	        //console.log("URL请求地址："+url);

	        //调用内部方法
	        active.showWin(titleStr,url,tW,tH);

		},
		/**
		 * 数据详情函数
		 */
		detail: function(title,id,tW=0,tH=0){
			//调用内部方法
	        var url = cUrl+"/detail?id="+id;
	        active.showWin(title+"详情",url,tW,tH);
		},
		/**
		 * 删除单条数据函数
		 */
		drop: function(id,callback) {
			layer.confirm('您确定要删除吗？删除后将无法恢复！', {
                icon: 3,
                skin: 'layer-ext-moon',
                btn: ['确认', '取消'] //按钮
            },function(index){
            	
            	//调用内部方法
            	var url = cUrl+"/drop";
            	active.ajaxPost(url,{"id":id},function(data,flag){
            		if(callback) {
            			console.log("删除成功ID:"+data.id);
            			callback(data,flag);
                	}
            	},'正在删除。。。');
            	
			});
			
		},
		/**
		 * 批量删除函数
		 */
		batchDrop: function(data,form,callback) {
			var ids = [];
			if(data.length > 0) {
	            for (var i in data) {
	                ids.push(data[i].id);
	            }
	            var idsStr = ids.join(",");
	            layer.confirm('确定删除选中的数据吗？', { icon: 3, title: '提示信息' }, function (index) {
	            	$.ajax({
	                    url:cUrl + "/batchDrop",
	                    dataType:"json",
	                    type:"POST",
	                    data:{"id":idsStr,"changeAct":0},
	                    beforeSend:function () {
	                        layer.msg('正在提交。。。', {
	                            icon: 16
	                            ,shade: 0.01
	                            ,time: 0
	                        });
	                        $('#'+form).attr('disabled',"true");
	                    },
	                    success:function(res){
	                        $('#'+form).removeAttr("disabled");
	                        layer.closeAll();
	                        if(res.success){
	                        	//2秒后关闭
	                            layer.msg(res.msg,{ icon: 1,time: 1000}, function () {
	                            	if(callback) {
	                            		callback(true);
	                            	}
	                            });
	                        }else{
	                            layer.msg(res.msg,{ icon: 5 });
	                        }
	                    },
	                    error:function() {
	                    	layer.msg("AJAX请求异常");
	                    	if(callback) {
	                    		callback(false);
	                    	}
	                    }
	                });
	            	
	            })
	        }else{
	            layer.msg("请选择需要删除的数据");
	        }
			
		},cache: function(id) {
            var url = cUrl + "/cache";
            active.ajaxPost(url,{id: id},(function(data, status) {}));
        },
        copy: function(title, id, tW=0,tH=0) {
            var url = cUrl + "/copy?id=" + id;
            active.showWin(title + "复制", url, tW, tH);
        },
        delete: function(id, callback=null) {
            layer.confirm("您确定要删除吗？删除后将无法恢复！", {
                icon: 3,
                skin: "layer-ext-moon",
                btn: ["确认", "取消"]
            }, (function(index) {
                    var url = cUrl + "/delete/" + id;
                    active.ajaxGet(url,{},function(data,status){
                        layer.close(index);
                        if(callback) {
                            callback(data,status);
                        }
                    },'正在删除。。。');
                }
            ))
        },
        batchFunc: function(n, callback=null) {
            var url = n.url, title = n.title
				,form = n.form
                , confirm = (n.confirm || false)
                , tips = n.show_tips || "处理中..."
                , data = n.data || []
                , param = n.param || []
                , type = n.type || "POST";
            if(title != "导出数据" &&  data.length == 0){
                layer.msg("请选择数据",{icon: 5});
                return false;
			}
            var ids = [];
            for (var i in data){
                ids.push(data[i].id);
			}
            var link = ids.join(","), postData = {};
            postData.ids = link;
            if (Array.isArray(param)){
                for (var i in param) {
                    var p = param[i].split("=");
                    postData[p[0]] = p[1]
                }
			}
            if(confirm){
                layer.confirm("您确定要【" + title + "】选中的数据吗?",{
                    icon: 3,title: "提示信息"
                },(function() {
                    if("POST" == type){
                        active.ajaxPost(url, postData, callback, tips);
                    }else{
                        active.ajaxGet(url + "/" + link, {}, callback, tips);
                    }
                }));
			}else{
            	if("POST" == type){
                    active.ajaxPost(url, postData, callback, tips);
				}else{
                    active.ajaxGet(url + "/" + link, {}, callback, tips);
				}
			}
        },
		/**
		 * 表单验证函数
		 */
		verify: function(){
			form.verify({
				//value：表单的值、item：表单的DOM对象
		        required: function(value, item) { 
					var title = $(item).data('title');
					if(!title) {
						//自动获取
						title = $(item).parents('.layui-inline').find('.layui-form-label').text();
						if(title.indexOf("：") >= 0 ) { 
							title = title.substring(0,title.Length-1);
						} 
					}
					//值为空时提示
					if(!value) {
						return $(item).attr('placeholder');
					}
				}
				,number: [/^[0-9]*$/, '请输入数字']
				,username: function(value, item){
					//特殊字符验证
					if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
						return title + '不能含有特殊字符';
				    }
					//下划线验证
				    if(/(^\_)|(\__)|(\_+$)/.test(value)){
				    	return title + '首尾不能出现下划线\'_\'';
				    }
				    //数字验证
				    if(/^\d+\d+\d$/.test(value)){
				    	return title + '不能全为数字';
				    }
				}
				//数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
				,pass: [/^[\S]{6,12}$/,'密码必须6到12位，且不能出现空格'] 
			})
		},
		/**
		 * 表单提交函数
		 */
		submitForm: function(data,url,callback) {
            //url = $("form").attr("action");
            if(url == null){
            	if(data.id == 0){
            		url = cUrl + "/add"
				}else{
                    url = cUrl + "/update"
				}
			}
			//var param = JSON.stringify(data.field);
			//console.log(data);
            active.ajaxPost(url,data,(function(res,status) {
            	 if(status){
                     setTimeout((function() {
                             var index = parent.layer.getFrameIndex(window.name);
                             parent.layer.close(index)
                         }
                     ), 500);
				 }
                 callback(res,status);
				 return false;
			}));
		},
		/**
		 * 搜索函数
		 */
		searchForm: function(table,data,tableList='') {
			if(tableList=='') {
				tableList = "tableList";
			}
			//执行重载
	        table.reload(tableList, {
	            page: {curr: 1},
	            where: data.field
	        });
		},
		/**
		 * 初始化日期组件函数
		 */
		initDate:function(item,callback){
			
			if (Array.isArray(item)) {
			    for (var i in item) {
			    	var subItem = item[i].split('|');
			    	if(subItem[2]) {
			    		var param = subItem[2].split(',');
			    	}
			    	
			    	//日期组件数据重组
			    	var options = {};
			    	options.elem = "#"+subItem[0];
			    	options.type = subItem[1];
			    	options.theme = 'molv';//主题颜色[molv,#393D49,grid]
			    	options.range = subItem[3]==="true" ? true : subItem[3];//开启左右面板
			    	options.calendar = true;//是否显示公历节日
			    	options.show = false;//默认显示
			    	options.position = 'absolute';//[fixed,absolute,static]
			    	options.trigger = 'click';//定义鼠标悬停时弹出控件[click,mouseover]
			    	options.btns = ['clear','now','confirm'];//工具按钮 默认值['clear', 'now', 'confirm']
			    	options.mark = {'0-06-25':"生日",'0-12-31':"跨年"};//自定义标注重要日子
			    	//控件在打开时触发，回调返回一个参数
			    	options.ready = function(date){
			    		console.log("组件面板打开："+date);
			    	}
			    	//日期时间被切换后的回调
			    	options.change = function(value, date, endDate){
			    		console.log(value); //得到日期生成的值，如：2017-08-18
			    	    console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
			    	    console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
			    	}
			    	//控件选择完毕后的回调
			    	options.done = function(value, date,endDate){
						if(callback) {
							callback(value, date);
						}
					}
			    	if(param) {
			    		//最小值
			    		var minV = param[0];
			    		if(minV) {
			    			var isNum = !isNaN(minV);
				    		if(isNum) {
				    			//数字
				    			options.min = parseInt(minV);
				    		}else{
				    			//非数字
				    			options.min = minV;
				    		}
			    		}
			    		//最大值
			    		var maxV = param[1];
			    		if(maxV) {
			    			var isNum2 = !isNaN(maxV);
				    		if(isNum2) {
				    			//数字
				    			options.max = parseInt(maxV);
				    		}else{
				    			//非数字
				    			options.max = maxV;
				    		}
			    		}
			    	}
			    	
			    	//日期选择组件
			    	laydate.render(options);
	            }
			}
		},
		/**
		 * 弹出窗体函数
		 */
		showWin:function(title,url,tW=0,tH=0,param,type=2,btn,callback){
	        var index = layui.layer.open({
	            title : title,
	            type : type,
	            area : [tW+"px",tH+"px"],
	            content : url,
	            shadeClose: true,//点击遮罩关闭  
	            shade: 0.4,
	            maxmin: true, //开启最大化最小化按钮
	            btn: btn,
		        btnAlign: 'c',
	            success : function(layero, index){
	            	//窗体传值【支持多值传递】
	            	if (Array.isArray(param)) {
	            		for (var i in param) {
	            			var item = param[i].split('=');
	            			console.log("传值："+item[0]+","+item[1]);
	            			var body = layui.layer.getChildFrame('body', index);
	                        body.find("#"+item[0]).val(item[1]);
	            		}
	            	}
	            	//回调函数
	                if(callback) {
	                	callback(layero,index);
	                }
	            	
	                //延迟5秒
	                setTimeout(function(){
	                    layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
	                        tips: 3
	                    });
	                },500);
	                
	            },
	            end: function () {
	                index = 0;
	            }
	        });
	        
	        if(tW==0) {
	        	//全屏设置
	        	layui.layer.full(index);
	            $(window).on("resize",function(){
	            	if(index != 0)
	            		layui.layer.full(index);
	            });
	        }

		},
		/**
		 * 网络请求函数(POST)
		 */
		ajaxPost:function(url,data,callback,msg='处理中,请稍后。。。'){
			var index = '';
			$.ajax({
                url:url,
                dataType:"json",
                contentType: 'application/json',
                type:"POST",
                data:JSON.stringify(data),
                beforeSend:function () {
                	index = layer.msg(msg, {
                        icon: 16
                        ,shade: 0.01
                        ,time: 0
                    });
                },
                success:function(res){
                    if(res.code == 0){
                    	//2秒后关闭
                        layer.msg(res.msg,{ icon: 1,time: 500}, function () {
            				layer.close(index);
            				if(callback) {
                        		callback(res,true);
                        	}
                        });
                    }else{
                        layer.msg(res.msg,{ icon: 5 });
                        if(callback) {
                            callback(res,false);
                        }
                        return false;
                    }
                },
                error:function() {
                	layer.msg("AJAX请求异常");
                	if(callback) {
                		callback("AJAX请求异常",false);
                	}
                }
            });
		},ajaxGet:function(url,data,callback,msg='处理中,请稍后。。。'){
            var index = '';
            $.ajax({
                url:url,
                dataType:"json",
                contentType: 'application/json',
                type:"GET",
                data:data,
                beforeSend:function () {
                    index = layer.msg(msg, {
                        icon: 16
                        ,shade: 0.01
                        ,time: 0
                    });
                },
                success:function(res){
                    if(res.code == 0){
                        //2秒后关闭
                        layer.msg(res.msg,{ icon: 1,time: 500}, function () {
                            layer.close(index);
                            if(callback) {
                                callback(res,true);
                            }
                        });
                    }else{
                        layer.msg(res.msg,{ icon: 5 });
                        if(callback) {
                            callback(res,false);
                        }
                        return false;
                    }
                },
                error:function() {
                    layer.msg("AJAX请求异常");
                    if(callback) {
                        callback("AJAX请求异常",false);
                    }
                }
            });
        },formSwitch: function(name, url, callback) {
            form.on("switch(" + name + ")", (function(e) {
                var r = this.checked ? "1" : "0";
                url = (url!=null)?url:cUrl + "/set" + name.substring(0, 1).toUpperCase() + name.substring(1);
                var data = {};
                data.id = this.value, data[name] = r;
                active.ajaxPost(url,data, (function(data, status) {
                    callback(data, status)
                }))
            }))
        }, uploadFile: function(elem, callback, url = "", exts = "xls|xlsx", size = 10240, data = {}) {
				if(url == ""){
                    url = cUrl + "/uploadFile";
				}
                r.render({
                    elem: "#" + elem,
                    url: url,
                    auto: false,
                    exts: exts,
                    accept: "file",
                    size: size,
                    method: "post",
                    data: data,
                    before: function(n) {
                        layer.msg("上传并处理中。。。", {
                            icon: 16,
                            shade: .01,
                            time: 0
                        })
                    },
                    done: function(res) {
                        layer.closeAll();
						if(res.code ==0){
                            layer.alert(res.msg, {
                                title: "上传反馈",
                                skin: "layui-layer-molv",
                                closeBtn: 1,
                                anim: 0,
                                btn: ["确定", "取消"],
                                icon: 6,
                                yes: function() {
                                    callback(res, true)
                                },
                                btn2: function() {}
                            })
						}else{
                            layer.msg(res.msg, {
                                icon: 5
                            })
						}
						return false;
                    },
                    error: function() {
                        return layer.msg("数据请求异常")
                    }
                })
        }
	}
	
	/**
	 * 输入自定义模块(此模块接口是对象)
	 */
    exports('com', active); 
});